define([],function(){
    App.myApp.onPageInit("page.login.page",function(){
        //App.myApp.hideToolbar(".toolbar");
    });
    var toast = App.toast;
    var send_code_num =0 ;
    var time_id =0 ;
    var sendcode_url = App.url("api/user/sendCode");
    var sms_login_url = App.url("api/user/sms_login");
    var login_url = App.url("api/user/login");
    var input = function(name){
        return $("input[name='"+name+"']").val();
    };
    G.login_input=function(){
        if(
            input("username") && input("password")
        ){
            $(".sub__button").removeClass("disable__button");
        }else{
            $(".sub__button").addClass("disable__button");
        }
    };
    G.sms_login_input=function(){
        if(
            input("phone") && input("code")
        ){
            $(".sms_sub__button").removeClass("disable__button");
        }else{
            $(".sms_sub__button").addClass("disable__button");
        }
    };
    var show_time = function(){
        if(send_code_num<1){
            $("#login_send_code_btn").attr("onclick","G.send_code()");
            $("#login_send_code_btn").html("获取验证码");
            clearTimeout(time_id);
        }else{
            $("#login_send_code_btn").attr("onclick","");
            send_code_num--;
            $("#login_send_code_btn").html("稍等"+send_code_num+"S");
            setTimeout(show_time,1000);
        }
    };
    G.send_code=function(){
        var phone = input("phone");
        if(!phone){
            alert("请输入电话");
        }
        App.get(sendcode_url,{
            phone:phone
        },function(d){
            if(d.code == 1000){
                send_code_num = 60;
                show_time();
            }else{
                toast(d.message);
            }
        });
    };
    G.login=function(){
        var username = input("username");
        var password = input("password");
        if(username && password){
            App.post(login_url,{
                username:username,
                password:password
            },function(d){
                if(d.code == 1000){
                    toast(d.message);
                }else{
                    toast(d.message);
                }
            });
        }
    };
    G.sms_login=function(){
        var phone = input("phone");
        var code = input("code");
        if(phone && code){
            App.post(sms_login_url,{
                phone:phone,
                code:code
            },function(d){
                if(d.code == 1000){
                    toast(d.message);
                }else{
                    toast(d.message);
                }
            });
        }
    };
   return {
       context:{}
   };
});